﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XChip_Common
{
   public class CommandTxt
    {
        //撤销表
        private List<ExecuteTxt> undoList;
        //重做表
        private List<ExecuteTxt> redoList;
        //最大撤销/重做次数
        private int undoCount = -1;
        //记录是否为撤销的记录，因为在execute函数里会造成重复
        private bool und = false;
        //记录当前需要撤销或回复的字符串
        private ExecuteTxt temp;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_undoCount"></param>
        public CommandTxt(int _undoCount, ExecuteTxt command)
        {
            undoList = new List<ExecuteTxt>();
            redoList = new List<ExecuteTxt>();
            //校正最大撤销/重做次数
            undoCount = _undoCount + 1;
            //上一句的原因
            undoList.Add(command);
        }
        public CommandTxt(int _undoCount)
        {
            undoList = new List<ExecuteTxt>();
            redoList = new List<ExecuteTxt>();
            //校正最大撤销/重做次数
            undoCount = _undoCount + 1;
            //上一句的原因
            //undoList.Add(new ExecuteTxt());
        }
        /// <summary>
        /// 更改
        /// </summary>
        /// <param name="command"></param>
        public void execute(ExecuteTxt command)
        {
            temp = command;
            if (!und)
            {
                undoList.Add(command);
                if (undoCount != -1 && undoList.Count > undoCount)
                {
                    undoList.RemoveAt(0);
                }
            }
            else
            {
                und = false;
            }
        }
        /// <summary>
        /// 撤销操作
        /// </summary>
        public void undo()
        {
            if (undoList.Count <=1)
                return;
            und = true;
            redoList.Add(undoList[undoList.Count - 1]);
            undoList.RemoveAt(undoList.Count - 1);
            temp = undoList[undoList.Count - 1];
        }
        /// <summary>
        /// 重置操作
        /// </summary>
        public void redo()
        {
            if (redoList.Count <= 0)
                return;
            temp = redoList[redoList.Count - 1];
            redoList.RemoveAt(redoList.Count - 1);
        }

        public ExecuteTxt Record
        {
            get { return temp; }
        }
        /// <summary>
        /// 判断是否可以进行Undo操作
        /// </summary>
        public bool CanUndo { get { return undoList.Count > 1; } }
        /// <summary>
        /// 判断是否可以进行Redo操作
        /// </summary>
        public bool CanRedo { get { return redoList.Count > 0; } }
    }


    public class ExecuteTxt {
        /// <summary>
        /// 主表选中行
        /// </summary>
        public int gridView1_RowHandle = 0;
        /// <summary>
        /// 从表选中行
        /// </summary>
        public int gridView2_RowHandle = 0;

        public string textEdit = string.Empty;
    }
}
